/*
 * Copyright (C) 2012 Martin Řehánek
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cz.mzk.funnylib.api;

import java.io.IOException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Martin Řehánek
 */
public class GoogleBooksSearcher {

    private static final String API_KEY = "AIzaSyAg0846miymBTw7QBv6uJ5aENqAs5-Fpik";

    public String searchTitleByIsbn(String isbn) {
        try {
            String request = constructUri(isbn);
            JSONObject json = HttpConnector.httpsGetRequest(request);
            return parseJson(json);
        } catch (JSONException ex) {
            return "nenalezeno";
        } catch (IOException e) {
            System.err.println(e.toString());
            return "nenalezeno";
        }
    }

    private String constructUri(String isbn) {
        StringBuilder builder = new StringBuilder();
        builder.append("https://www.googleapis.com/books/v1/volumes?q=isbn:");
        builder.append(isbn);
        return builder.toString();
    }

    private String parseJson(JSONObject requestResult) {
        try {
            JSONArray item = requestResult.getJSONArray("items");
            //List<Book> result = new ArrayList<Book>(item.length());
            if (item.length() > 0) {
                try {
                    JSONObject json = item.getJSONObject(0);
                    // Book book = new Book();
                    JSONObject volumeInfo = json.getJSONObject("volumeInfo");
                    // title
                    String title = volumeInfo.getString("title");
                    return title;
                    // book.setTitle(title);
                    // author
//                    JSONArray authors = volumeInfo.getJSONArray("authors");
//                    if (authors.length() != 0) {
//                        book.setAuthor(authors.getString(0));
//                    }
//                    // published
//                    String published = volumeInfo.getString("publishedDate");
//                    book.setPublishDate(published);
//                    // image
//                    book.setThumbnailURL(findImageUrl(volumeInfo));
//                    // isbn 10
//                    book.setIsbn(findIsbn(volumeInfo));
//                    // zdrojove url
//                    book.setOriginalUrl(findOriginalUrl(volumeInfo));
//                    result.add(book);
                } catch (JSONException e) {
                    return "nenalezeno";
                }
            } else {
                return "nenalezeno";
            }
            // Book testBook = new Book();
            // testBook.setAuthor("items: " + item.length());
            // result.add(testBook);
            //return result;
        } catch (JSONException e) {
            return "nenalezeno";
        }
    }
}
